import Vue from 'vue';
import {setLocalStorage,getLocalStorage,parseQueryString} from './index';
import {config} from './weixin_handler';

//页面初始化执行的函数
let pageOnShow = async function(){
	let userInfo = Vue.prototype.$store.getters['outer_me/userInfo'];
	//#ifdef H5
	var globalConfigObj = getLocalStorage('config');
	if(globalConfigObj && globalConfigObj.shop_name){
		document.title = globalConfigObj.shop_name; 
	}else{
		await Vue.prototype.$store.dispatch('outer_checklogin/getConfig');
		globalConfigObj = getLocalStorage('config');
		if(globalConfigObj && globalConfigObj.shop_name){
			document.title = globalConfigObj.shop_name; 
		};
	};
	if(!userInfo.id){
		await Vue.prototype.$store.dispatch('outer_me/getUserCenterInfoUrl', false);
		userInfo = Vue.prototype.$store.getters['outer_me/userInfo'];
	};
	// console.log(userInfo, 1111)
	let url;
	if(window.location.search) {
		url = window.location.href + '&refer_id='+userInfo.id+'&mall_id='+getLocalStorage('mall_id') + '&sid=0';
	}else{
		url = window.location.href + '?refer_id='+userInfo.id+'&mall_id='+getLocalStorage('mall_id') + '&sid=0';
	}
	// if(!debug){
		await config(url, false);
	// }
	let query = parseQueryString(location.href);
	if(query.refer_id && !userInfo.refer_id) {
		//绑定推荐人
		await Vue.prototype.$store.dispatch('outer_me/bindRefer', query.refer_id);
	};
	//#endif
};



module.exports = {
	pageOnShow
};